﻿Imports TransportadoraToledoDAL
Public Class ModelosBLL
    Public Sub New()

    End Sub
    Dim objDAL As ModelosDAL
#Region "Propriedades"
    Private mod_codigo As Integer
    Public Property pMod_codigo() As Integer
        Get
            Return mod_codigo
        End Get
        Set(ByVal value As Integer)
            mod_codigo = value
        End Set
    End Property
    Private mar_codigo As Integer
    Public Property pMar_codigo() As Integer
        Get
            Return mar_codigo
        End Get
        Set(ByVal value As Integer)
            mar_codigo = value
        End Set
    End Property
    Private mod_nome As String
    Public Property pMod_nome() As String
        Get
            Return mod_nome
        End Get
        Set(ByVal value As String)
            mod_nome = value
        End Set
    End Property
#End Region
#Region "Métodos"
    Public Function inserir() As Boolean
        Dim ret As Boolean = False
        Try
            If (Me.mar_codigo = 0) Then
                Throw New Exception("Informe uma marca para o modelo")
            ElseIf (Me.mod_nome = "") Then
                Throw New Exception("Informe o nome do modelo")
            Else
                objDAL = New ModelosDAL
                objDAL.inserir(Me.mar_codigo, Me.mod_nome)
                ret = True
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return ret
    End Function
    Public Function alterar() As Boolean
        Dim ret As Boolean = False
        Try
            If (Me.mar_codigo = 0) Then
                Throw New Exception("Informe uma marca para o modelo")
            ElseIf (Me.mod_nome = "") Then
                Throw New Exception("Informe o nome do modelo")
            ElseIf (Me.mod_codigo = 0) Then
                Throw New Exception("O Modelo não pode ser alterado")
            Else
                objDAL = New ModelosDAL
                objDAL.alterar(Me.mar_codigo, Me.mod_nome, Me.mod_codigo)
                ret = True
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return ret
    End Function
    Public Function excluir() As Boolean
        Dim ret As Boolean = False
        Try
            If (Me.mod_codigo = 0) Then
                Throw New Exception("Modelo não pode ser excluido")
            Else
                objDAL = New ModelosDAL
                objDAL.excluir(Me.mod_codigo)
                ret = True
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return ret
    End Function
    Function localizar(ByVal nome As String, ByVal marca As Integer) As DataTable
        Dim tab As DataTable = Nothing
        Try
            objDAL = New ModelosDAL
            tab = objDAL.localizar(nome, marca)

        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function

    Function localizar(ByVal nome As String) As DataTable
        Dim tab As DataTable = Nothing
        Try
            objDAL = New ModelosDAL
            tab = objDAL.localizar(nome)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function
    'retorna todas as linhas pesquisadas da tabela
    Function localizarPrimeiroUltimo(ByVal nome As String) As Integer
        Dim tab As DataTable = Nothing
        Dim codigo As Integer = 0
        Try
            If (nome = "ultimo") Then
                objDAL = New ModelosDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("mod_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("mod_codigo").ToString
                End If
            ElseIf (nome = "primeiro") Then
                objDAL = New ModelosDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("mod_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("mod_codigo").ToString
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return codigo
    End Function

    Function localizarProxAnteriro(ByVal nome As String, ByVal codigo As Integer) As Integer
        Dim tab As DataTable = Nothing
        Dim CodigoP As Integer = 0
        Try
            objDAL = New ModelosDAL
            tab = objDAL.localizarProxAnterior(nome, codigo)
            If (tab.Rows.Count > 0) Then
                CodigoP = tab.Rows(0)("mod_codigo").ToString
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return CodigoP
    End Function

    Public Sub localizar(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New ModelosDAL
            tab = objDAL.localizar(codigo)
            If (tab.Rows.Count = 1) Then
                Me.mar_codigo = tab.Rows(0)("mar_codigo").ToString()
                Me.mod_codigo = tab.Rows(0)("mod_codigo").ToString()
                Me.mod_nome = tab.Rows(0)("mod_nome").ToString()
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Public Sub localizaMarca(ByVal codigoModelo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New ModelosDAL
            tab = objDAL.localizaMarca(codigoModelo)
            If (tab.Rows.Count = 1) Then
                Me.mar_codigo = tab.Rows(0)("mar_codigo").ToString()
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Public Sub localizaMarcaEx(ByVal codigoMarca As Integer)
        Dim tab As DataTable
        Try
            objDAL = New ModelosDAL
            tab = objDAL.localizaMarcaEx(codigoMarca)
            If (tab.Rows.Count = 1) Then
                Me.mar_codigo = tab.Rows(0)("mar_codigo").ToString()
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub
#End Region
End Class
